package cn.zjweu.dao.impl;

import cn.zjweu.dao.UserDao;
import cn.zjweu.domain.MessageInfo;
import cn.zjweu.domain.User;
import cn.zjweu.utils.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;

    private void initThree() throws SQLException {
        conn = JDBCUtil.getConnection();
        pst = null;
        rs = null;
    }

    private void initTwo() throws SQLException {
        conn = JDBCUtil.getConnection();
        pst = null;
    }

    @Override
    public User findUserByNameAndPWD(String username, String password) {
        User user = null;
        String sql = "SELECT * FROM user WHERE username = ? AND password = ?";

        try {
            initThree();
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            pst.setString(2, password);
            System.out.println(pst.toString());
            rs = pst.executeQuery();
            while (rs.next()) {
                if (rs.getString("username") != null) {
                    user = new User();
                    user.setUserId(rs.getInt("userId"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pst, conn);
        }
        return user;
    }

    @Override
    public User findUsername(String username) {
        User user = null;
        String sql = "SELECT * FROM user WHERE username = ?";

        try {
            initThree();
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            System.out.println(pst.toString());
            rs = pst.executeQuery();
            while (rs.next()) {
                if (rs.getString("username") != null) {
                    user = new User();
                    user.setUserId(rs.getInt("userId"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setTrueName(rs.getString("trueName"));
                    user.setTelephone(rs.getString("telephone"));
                    user.setCity(rs.getString("city"));
                    user.setAddress(rs.getString("address"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pst, conn);
        }
        return user;
    }

    @Override
    public boolean saveUser(String username, String password, String trueName, String telephone, String city, String address) {
        boolean flag = MessageInfo.SUCCESS;
        String sql = "INSERT INTO user VALUES(null,?,?,?,?,?,?)";

        try {
            initTwo();
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            pst.setString(2, password);
            pst.setString(3, trueName);
            pst.setString(4, telephone);
            pst.setString(5, city);
            pst.setString(6, address);
            System.out.println(pst.toString());
            pst.executeUpdate();
        } catch (SQLException e) {
            flag = MessageInfo.FAIL;
            e.printStackTrace();
        } finally {
            JDBCUtil.close(pst, conn);
        }
        return flag;
    }

    @Override
    public boolean editUser(User user) {
        String sql = "UPDATE user SET " +
                "username=?, trueName=?, telephone=?, city=?, address=? WHERE userId=?";
        try {
            initTwo();
            pst = conn.prepareStatement(sql);
            pst.setString(1, user.getUsername());
            pst.setString(2, user.getTrueName());
            pst.setString(3, user.getTelephone());
            pst.setString(4, user.getCity());
            pst.setString(5, user.getAddress());
            pst.setInt(6, user.getUserId());
            pst.executeUpdate();
        } catch (SQLException e) {
            return MessageInfo.FAIL;
        } finally {
            JDBCUtil.close(pst, conn);
        }
        return MessageInfo.SUCCESS;
    }

    @Override
    public User findUserById(int userId) {
        User user = null;
        String sql = "SELECT * FROM user WHERE userId = ?";

        try {
            initThree();
            pst = conn.prepareStatement(sql);
            pst.setInt(1, userId);
            System.out.println(pst.toString());
            rs = pst.executeQuery();
            while (rs.next()) {
                if (rs.getString("username") != null) {
                    user = new User();
                    user.setUserId(rs.getInt("userId"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setTrueName(rs.getString("trueName"));
                    user.setTelephone(rs.getString("telephone"));
                    user.setCity(rs.getString("city"));
                    user.setAddress(rs.getString("address"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pst, conn);
        }
        return user;
    }

    @Override
    public List<User> findAllUser() {
        String sql = "SELECT * FROM user";
        List<User> userList = new ArrayList<>();
        try {
            initThree();
            pst = conn.prepareStatement(sql);
            System.out.println(pst);
            rs = pst.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setUserId(rs.getInt("userId"));
                user.setUsername(rs.getString("username"));
                user.setTrueName(rs.getString("trueName"));
                user.setTelephone(rs.getString("telephone"));
                user.setCity(rs.getString("city"));
                user.setAddress(rs.getString("address"));
                userList.add(user);
            }
            return userList;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pst, conn);
        }
        return null;
    }

    @Override
    public void resetPWD(int userId) {
        String sql = "UPDATE user SET password='123456' WHERE userId = ?";
        try {
            initTwo();
            pst = conn.prepareStatement(sql);
            pst.setInt(1, userId);
            System.out.println(pst);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(pst, conn);
        }
    }

    @Override
    public void delUserById(int userId) {
        String sql = "DELETE FROM user WHERE userId = ?";
        try {
            initTwo();
            pst = conn.prepareStatement(sql);
            pst.setInt(1, userId);
            System.out.println(pst);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(pst, conn);
        }
    }
}
